package com.fortune.controller;

import com.fortune.base.BaseAPIExcution;
import com.fortune.common.paging.PagingData;
import com.fortune.constants.Constants;
import com.fortune.dto.excution.CPriceMgtExcution;
import com.fortune.dto.in.cPriceMgt.CPriceAddDto;
import com.fortune.dto.in.cPriceMgt.CPriceEditDto;
import com.fortune.dto.in.cPriceMgt.CPriceListDto;
import com.fortune.dto.in.cPriceMgt.CWholesalePriceDto;
import com.fortune.entity.DUserInfo;
import com.fortune.enums.CPriceMgtEnum;
import com.fortune.service.CProductWholesalePriceService;
import com.fortune.service.ClientService;
import com.fortune.vo.cPriceMgt.CClientList;
import com.fortune.vo.cPriceMgt.CClientPrice;
import com.fortune.vo.cPriceMgt.CClientWholesalePrice;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.hibernate.validator.constraints.NotBlank;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Api(value = "cPriceMgt", description="经销商对客户批发价管理", tags = {"经销商对客户批发价管理"})
@RestController
@RequestMapping("cPriceMgt")
@Validated
public class CPriceMgtController{
	@Autowired
	private CProductWholesalePriceService cProductWholesalePriceService;
	@Autowired
	private ClientService clientService;

	@ApiOperation(value = "客户列表", nickname = "clientList", notes = "", tags = {"@石匡"})
	@GetMapping(value = "clientList",
			produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<List<CClientList>> clientList(HttpServletRequest request) {
		HttpSession session = request.getSession();
		DUserInfo dUserInfo = (DUserInfo) session.getAttribute(Constants.SESSION_D_USER);
		String companyId = dUserInfo.getmId();
		String dealerId = dUserInfo.getdId();
		List<CClientList> lists = clientService.queryClientList(companyId,dealerId);
		return new CPriceMgtExcution<List<CClientList>>(lists, CPriceMgtEnum.SUCCESS);
	}
	
	@ApiOperation(value = "客户批发价列表", nickname = "cPriceList", notes = "", tags = {"@石匡"})
	@ApiImplicitParams({
		@ApiImplicitParam(name="page", value="第几页",paramType="query",dataType="int"),
		@ApiImplicitParam(name="length", value="每页容量",paramType="query",dataType="int"),
		@ApiImplicitParam(name="clientId", value="客户ID",paramType="query",dataType="string"),
		@ApiImplicitParam(name="search", value="搜索关键词",paramType="query",dataType="string")
	})
    @GetMapping(value = "list",
        produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<PagingData<CClientPrice>> list(HttpServletRequest request,@ApiIgnore @Validated CPriceListDto listDto) {
		HttpSession session = request.getSession();
		DUserInfo dUserInfo = (DUserInfo) session.getAttribute(Constants.SESSION_D_USER);
		String companyId = dUserInfo.getmId();
		String dealerId = dUserInfo.getdId();
		listDto.setPage((listDto.getPage()-1)*10);
		List<CClientPrice> list = cProductWholesalePriceService.queryProductPriceList(companyId,dealerId,listDto);
		int count = cProductWholesalePriceService.queryProductPriceListCount(companyId,dealerId,listDto);
		PagingData<CClientPrice> pagingData = new PagingData<>();
		pagingData.setData(list);
		pagingData.setRecordsTotal(count);
		return new CPriceMgtExcution<PagingData<CClientPrice>>(pagingData, CPriceMgtEnum.SUCCESS);
	}

	@ApiOperation(value = "添加客户批发价", nickname = "cPriceAdd", notes = "", tags = {"@石匡"})
    @PostMapping(value = "add",
        produces = { MediaType.APPLICATION_JSON_VALUE },
        consumes = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> add(HttpServletRequest request,@Validated @RequestBody CPriceAddDto addDto) {
		HttpSession session = request.getSession();
		DUserInfo dUserInfo = (DUserInfo) session.getAttribute(Constants.SESSION_D_USER);
		String companyId = dUserInfo.getmId();
		String dealerId = dUserInfo.getdId();
		int num = cProductWholesalePriceService.addProductWholesalePrice(companyId,dealerId,addDto);
		if (num < 1){
			return new CPriceMgtExcution<Void>(null, CPriceMgtEnum.FAILURE);
		}
		return new CPriceMgtExcution<Void>(null, CPriceMgtEnum.SUCCESS);
	}
	
	@ApiOperation(value = "编辑客户批发价", nickname = "cPriceEdit", notes = "", tags = {"@石匡"})
    @PutMapping(value = "edit",
        produces = { MediaType.APPLICATION_JSON_VALUE },
        consumes = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> edit(HttpServletRequest request,@Validated @RequestBody CPriceEditDto editDto) {
		HttpSession session = request.getSession();
		DUserInfo dUserInfo = (DUserInfo) session.getAttribute(Constants.SESSION_D_USER);
		String companyId = dUserInfo.getmId();
		String dealerId = dUserInfo.getdId();
		int num = cProductWholesalePriceService.editProductWholesalePrice(companyId,dealerId,editDto);
		if (num < 1){
			return new CPriceMgtExcution<Void>(null, CPriceMgtEnum.FAILURE);
		}
		return new CPriceMgtExcution<Void>(null, CPriceMgtEnum.SUCCESS);
	}
	
	@ApiOperation(value = "删除客户批发价", nickname = "cPriceDelete", notes = "", tags = {"@石匡"})
	@ApiImplicitParams({
		@ApiImplicitParam(name="productId", value="商品id",paramType="query",dataType="string"),
		@ApiImplicitParam(name="clientId", value="客户id",paramType="query",dataType="string")
	})
    @DeleteMapping(value = "delete",
        produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> delete(HttpServletRequest request,@Validated @NotBlank(message="商品id不能为空") String productId,
			@Validated @NotBlank(message="客户id不能为空") String clientId) {
		HttpSession session = request.getSession();
		DUserInfo dUserInfo = (DUserInfo) session.getAttribute(Constants.SESSION_D_USER);
		String companyId = dUserInfo.getmId();
		String dealerId = dUserInfo.getdId();
		int num = cProductWholesalePriceService.deleteProductWholesalePrice(companyId,dealerId,productId,clientId);
		if (num < 1){
			return new CPriceMgtExcution<Void>(null, CPriceMgtEnum.FAILURE);
		}
		return new CPriceMgtExcution<Void>(null, CPriceMgtEnum.SUCCESS);
	}

	@ApiOperation(value = "商品统一批发价", nickname = "wholesalePrice", notes = "", tags = {"@石匡"})
	@ApiImplicitParams({
			@ApiImplicitParam(name="page", value="第几页",paramType="query",dataType="int"),
			@ApiImplicitParam(name="length", value="每页容量",paramType="query",dataType="int")
	})
	@GetMapping(value = "wholesalePrice",
			produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<PagingData<CClientWholesalePrice>> wholesalePrice(HttpServletRequest request, @ApiIgnore @Validated CWholesalePriceDto cWholesalePriceDto) {
		HttpSession session = request.getSession();
		DUserInfo dUserInfo = (DUserInfo) session.getAttribute(Constants.SESSION_D_USER);
		String companyId = dUserInfo.getmId();
		String dealerId = dUserInfo.getdId();
		cWholesalePriceDto.setPage((cWholesalePriceDto.getPage()-1)*10);
		List<CClientWholesalePrice> list = cProductWholesalePriceService.queryProductWholesalePriceList(companyId,dealerId,cWholesalePriceDto);
		int count = cProductWholesalePriceService.queryProductWholesalePriceListCount(companyId,dealerId,cWholesalePriceDto);
		PagingData<CClientWholesalePrice> pagingData = new PagingData<>();
		pagingData.setData(list);
		pagingData.setRecordsTotal(count);
		return new CPriceMgtExcution<PagingData<CClientWholesalePrice>>(pagingData, CPriceMgtEnum.SUCCESS);
	}
}
